package com.aiti.lulian.strategy.task;



import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardUserIncreaseRankDto;
import com.aiti.lulian.entity.CardEnterpriseTemplate;
import com.aiti.lulian.entity.CardRankTaskUserComplete;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.enums.RankIncreaseTaskStrategyEnum;
import com.aiti.lulian.mapper.CardRankTaskUserCompleteMapper;
import com.aiti.lulian.mapper.CardTemplateStyleFieldMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardEnterpriseTemplateService;
import com.aiti.lulian.service.CardUserRankTaskHistoryService;
import com.aiti.lulian.vo.CardRankTaskUserVo;
import com.aiti.lulian.vo.CardTemplateStyleFieldAndDataVo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Component
public class EnterpriseCardAvatarAndBaseInfoStrategy implements RankIncreaseTaskStrategy {
    @Autowired
    private CardRankTaskUserCompleteMapper cardRankTaskUserCompleteMapper;
    @Autowired
    private CardUserRankTaskHistoryService cardUserRankTaskHistoryService;
    @Autowired
    private CardPersonalBaseInfoMapper cardBaseInfoMapper;
    @Autowired
    private CardEnterpriseTemplateService cardEnterpriseTemplateService;
    @Autowired
    private CardTemplateStyleFieldMapper cardTemplateStyleFieldMapper;

    @Override
    public RankIncreaseTaskStrategyEnum getStrategy() {
        return RankIncreaseTaskStrategyEnum.ENTERPRISE_CARD_AVATAR_AND_BASE_INFO;
    }

    @Override
    public void handleTask(CardRankTaskUserVo vo, CardUserIncreaseRankDto dto) {
        CardBaseInfo cardBaseInfo = cardBaseInfoMapper.selectById(dto.getCardId());
        String templateId = cardBaseInfo.getTemplateId();
        String cardId = cardBaseInfo.getBaseId();
        if(StringUtils.isEmpty(templateId)){
            vo.setDoneFlag(false);
        }else{
            List<CardEnterpriseTemplate> templateList = cardEnterpriseTemplateService.list(new LambdaQueryWrapper<CardEnterpriseTemplate>().eq(CardEnterpriseTemplate::getNewTemplateId, templateId));
            if(!CollectionUtils.isEmpty(templateList)){
                String subTemplateId = templateList.get(0).getBaseId();
                List<CardTemplateStyleFieldAndDataVo> cardTemplateStyleFieldAndDataVos = cardTemplateStyleFieldMapper.queryStyleFieldAndDataByCondition(cardId, subTemplateId);
                if(CollectionUtils.isEmpty(cardTemplateStyleFieldAndDataVos)){
                    vo.setDoneFlag(false);
                }else{
                    boolean completeFlag = false;
                    for(CardTemplateStyleFieldAndDataVo data:cardTemplateStyleFieldAndDataVos){
                        String extraInfo = data.getExtraInfo();
                        JSONObject jsonObject = JSONObject.parseObject(extraInfo);
                        Object text = jsonObject.get("text");
                        String styleFieldValue = data.getStyleFieldValue();
                        if(!StringUtils.isEmpty(styleFieldValue) && !styleFieldValue.equals(text)){
                            completeFlag = true;
                        }
                    }
                    if(!completeFlag){
                        vo.setDoneFlag(false);
                    }else{
                        vo.setDoneFlag(true);
                    }
                }



            }
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message handle(CardUserIncreaseRankDto dto) {
        String userId = dto.getUserId();
        String cardId = dto.getCardId();
        List<CardRankTaskUserVo> cardRankTaskUserVos = getCompletedTask(dto);
        if(CollectionUtils.isEmpty(cardRankTaskUserVos)){
            return Message.fail("任务不存在");
        }else{
            if(cardRankTaskUserVos.get(0).isCompleteFlag()) {//已完成
                return Message.fail("该用户已完成过该任务，请勿重复");
            }else{
                CardBaseInfo cardBaseInfo = cardBaseInfoMapper.selectById(cardId);
                String templateId = cardBaseInfo.getTemplateId();
                if(StringUtils.isEmpty(templateId)){
                    return Message.fail("该名片对应的名片模板不存在，请先添加名片模板");
                }
                List<CardEnterpriseTemplate> templateList = cardEnterpriseTemplateService.list(new LambdaQueryWrapper<CardEnterpriseTemplate>().eq(CardEnterpriseTemplate::getNewTemplateId, templateId));
                if(CollectionUtils.isEmpty(templateList)){
                    return Message.fail("该名片对应的名片模板不存在，请先添加名片模板");
                }
                String subTemplateId = templateList.get(0).getBaseId();
                List<CardTemplateStyleFieldAndDataVo> cardTemplateStyleFieldAndDataVos = cardTemplateStyleFieldMapper.queryStyleFieldAndDataByCondition(cardId, subTemplateId);
                if(CollectionUtils.isEmpty(cardTemplateStyleFieldAndDataVos)){
                    return Message.fail("该名片模板未添加字段，请先添加");
                }
                boolean completeFlag = false;
                for(CardTemplateStyleFieldAndDataVo vo:cardTemplateStyleFieldAndDataVos){
                    String extraInfo = vo.getExtraInfo();
                    JSONObject jsonObject = JSONObject.parseObject(extraInfo);
                    Object text = jsonObject.get("text");
                    String styleFieldValue = vo.getStyleFieldValue();
                    if(!StringUtils.isEmpty(styleFieldValue) && !styleFieldValue.equals(text)){
                        completeFlag = true;
                    }
                }
                if(!completeFlag){
                    return Message.fail("请完善名片信息再领取");
                }

                
                CardRankTaskUserComplete cardRankTaskUserComplete = new CardRankTaskUserComplete();
                cardRankTaskUserComplete.setCardId(cardId);
                cardRankTaskUserComplete.setUserId(userId);
                cardRankTaskUserComplete.setTaskId(cardRankTaskUserVos.get(0).getBaseId());
                cardRankTaskUserCompleteMapper.insert(cardRankTaskUserComplete);
//                LambdaQueryWrapper<CardUserRankTaskHistory> wrapper = new LambdaQueryWrapper<CardUserRankTaskHistory>().eq(CardUserRankTaskHistory::getCardId, cardId)
//                        .eq(CardUserRankTaskHistory::getType, RankIncreaseTaskStrategyEnum.REAL_NAME_AUTH.getType());
//                List<CardUserRankTaskHistory> list = cardUserRankTaskHistoryService.list(wrapper);
//                if(!CollectionUtils.isEmpty(list)){
//                    return Message.fail("该用户已领取过奖励，请勿重复领取");
//                }


                Long score = cardRankTaskUserVos.get(0).getScore();
                dto.setScore(score);
                increseScore(dto);
                recordHistory(dto);


                return Message.success();
            }
        }
    }

    @Override
    public CardRankTaskUserCompleteMapper getCardRankTaskUserCompleteMapper() {
        return cardRankTaskUserCompleteMapper;
    }

    @Override
    public CardPersonalBaseInfoMapper getBaseInfoMapper() {
        return cardBaseInfoMapper;
    }

    @Override
    public CardUserRankTaskHistoryService getHisotryService() {
        return cardUserRankTaskHistoryService;
    }


}